{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "7fPc-KWUi2Xd"
      },
      "outputs": [],
      "source": [
        "# Copyright 2020 Google LLC\n",
        "#\n",
        "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "# you may not use this file except in compliance with the License.\n",
        "# You may obtain a copy of the License at\n",
        "#\n",
        "#     https://www.apache.org/licenses/LICENSE-2.0\n",
        "#\n",
        "# Unless required by applicable law or agreed to in writing, software\n",
        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "# See the License for the specific language governing permissions and\n",
        "# limitations under the License."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "SL_AnuSKi2Xj"
      },
      "source": [
        "<table align=\"left\">\n",
        "    <td>\n",
        "        <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/ai-platform-samples/blob/master/notebooks/samples/optimizer/ai_platform_optimizer_multi_objective.ipynb\">\n",
        "            <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
        "        </a>\n",
        "    </td>\n",
        "    <td>\n",
        "        <a href=\"https://github.com/GoogleCloudPlatform/ai-platform-samples/blob/master/notebooks/samples/optimizer/ai_platform_optimizer_multi_objective.ipynb\">\n",
        "            <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\">View on GitHub\n",
        "        </a>\n",
        "     </td>\n",
        "</table>\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "AksIKBzZ-nre"
      },
      "source": [
        "# Optimizing multiple objectives\n",
        "\n",
        "This tutorial demonstrates AI Platform Optimizer multi-objective optimization.\n",
        "\n",
        "### Objective\n",
        "\n",
        "The goal is to __`minimize`__ the objective metric:\n",
        "   ```\n",
        "   y1 = r*sin(theta)\n",
        "   ```\n",
        "\n",
        "and simultaneously __`maximize`__ the objective metric:\n",
        "   ```\n",
        "   y2 = r*cos(theta)\n",
        "   ```\n",
        "\n",
        "that you will evaluate over the parameter space:\n",
        "\n",
        "   - __`r`__ in [0,1],\n",
        "\n",
        "   - __`theta`__ in [0, pi/2]\n",
        "\n",
        "### Costs\n",
        "\n",
        "This tutorial uses billable components of Google Cloud:\n",
        "\n",
        "* AI Platform Training\n",
        "* Cloud Storage\n",
        "\n",
        "Learn about [AI Platform Training\n",
        "pricing](https://cloud.google.com/ai-platform/training/pricing) and [Cloud Storage\n",
        "pricing](https://cloud.google.com/storage/pricing), and use the [Pricing\n",
        "Calculator](https://cloud.google.com/products/calculator/)\n",
        "to generate a cost estimate based on your projected usage.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "D3LI2DGWi2Xl"
      },
      "source": [
        "### PIP install packages and dependencies\n",
        "\n",
        "Install additional dependencies not installed in the notebook environment.\n",
        "\n",
        "- Use the latest major GA version of the framework."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "2DOjox58i2Xm"
      },
      "outputs": [],
      "source": [
        "! pip install -U google-cloud\n",
        "! pip install -U google-cloud-storage\n",
        "! pip install -U requests"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "iMHz63rPbq6P"
      },
      "source": [
        "#### Install tensorflow-cloud\n",
        "\n",
        "Download and install tensorflow-cloud."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "g7WlujNxbq6Q"
      },
      "outputs": [],
      "source": [
        "! pip install tensorflow-cloud"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "O8AIwN0abq6U"
      },
      "outputs": [],
      "source": [
        "# Restart the kernel after pip installs\n",
        "import IPython\n",
        "app = IPython.Application.instance()\n",
        "app.kernel.do_shutdown(True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Xc9LnICki2Xp"
      },
      "source": [
        "### Set up your Google Cloud project\n",
        "\n",
        "**The following steps are required, regardless of your notebook environment.**\n",
        "\n",
        "1. [Select or create a Google Cloud project.](https://console.cloud.google.com/cloud-resource-manager)\n",
        "\n",
        "2. [Make sure that billing is enabled for your project.](https://cloud.google.com/billing/docs/how-to/modify-project)\n",
        "\n",
        "3. [Enable the AI Platform APIs](https://console.cloud.google.com/flows/enableapi?apiid=ml.googleapis.com)\n",
        "\n",
        "4. If running locally on your own machine, you will need to install the [Google Cloud SDK](https://cloud.google.com/sdk).\n",
        "\n",
        "5. Enter your project ID in the cell below. Then run the  cell to make sure the\n",
        "Cloud SDK uses the right project for all the commands in this notebook.\n",
        "\n",
        "**Note**: Jupyter runs lines prefixed with `!` as shell commands, and it interpolates Python variables prefixed with `$` into these commands."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "h0SMyUsC-mzi"
      },
      "source": [
        "### Authenticate your Google Cloud account\n",
        "\n",
        "**If you are using [AI Platform Notebooks](https://cloud.google.com/ai-platform/notebooks/docs/)**, your environment is already\n",
        "authenticated. Skip these steps."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "iTQY9g4mRo6r"
      },
      "outputs": [],
      "source": [
        "import sys\n",
        "\n",
        "# If you are running this notebook in Colab, run this cell and follow the\n",
        "# instructions to authenticate your Google Cloud account. This provides access\n",
        "# to your Cloud Storage bucket and lets you submit training jobs and prediction\n",
        "# requests.\n",
        "\n",
        "if 'google.colab' in sys.modules:\n",
        "    from google.colab import auth as google_auth\n",
        "    google_auth.authenticate_user()\n",
        "\n",
        "# If you are running this tutorial in a notebook locally, replace the string\n",
        "# below with the path to your service account key and run this cell to\n",
        "# authenticate your Google Cloud account.\n",
        "else:\n",
        "    %env GOOGLE_APPLICATION_CREDENTIALS your_path_to_credentials.json\n",
        "\n",
        "# Log in to your account on Google Cloud\n",
        "!gcloud auth login"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Dax2zrpTi2Xy"
      },
      "source": [
        "### Import libraries and define constants"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "xD60d6Q0i2X0"
      },
      "outputs": [],
      "source": [
        "import json\n",
        "import datetime\n",
        "from tensorflow_cloud.tuner import tuner\n",
        "from tensorflow_cloud.tuner import utils"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "CWuu4wmki2X3"
      },
      "source": [
        "## Tutorial\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "KyEjqIdnad0w"
      },
      "source": [
        "### Setup\n",
        "This section defines some parameters and util methods to call AI Platform Optimizer APIs. Please fill in the following information to get started."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "8HCgeF8had77"
      },
      "outputs": [],
      "source": [
        "# Fill in your username and project ID\n",
        "USER = '[user-id]' #@param {type: 'string'}\n",
        "PROJECT_ID = '[your-project-id]' #@param {type:\"string\"}\n",
        "\n",
        "# These will be automatically filled in.\n",
        "STUDY_ID = '{}_study_{}'.format(USER.replace('-', ''), datetime.datetime.now().strftime('%Y%m%d_%H%M%S')) #@param {type: 'string'}\n",
        "REGION = 'us-central1'\n",
        "! gcloud config set project $PROJECT_ID"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "8NBduXsEaRKr"
      },
      "source": [
        "### Create the study configuration\n",
        "\n",
        "The following is a sample study configuration, built as a hierarchical python dictionary. It is already filled out. Run the cell to configure the study."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "s-AHfPOASXXW"
      },
      "outputs": [],
      "source": [
        "# Parameter Configuration\n",
        "param_r = {\n",
        "    'parameter': 'r',\n",
        "    'type': 'DOUBLE',\n",
        "    'double_value_spec': {\n",
        "        'min_value': 0,\n",
        "        'max_value': 1\n",
        "    }\n",
        "}\n",
        "\n",
        "param_theta = {\n",
        "    'parameter': 'theta',\n",
        "    'type': 'DOUBLE',\n",
        "    'double_value_spec': {\n",
        "        'min_value': 0,\n",
        "        'max_value': 1.57\n",
        "    }\n",
        "}\n",
        "\n",
        "# Objective Metrics\n",
        "metric_y1 = {\n",
        "    'metric': 'y1',\n",
        "    'goal': 'MINIMIZE'\n",
        "}\n",
        "\n",
        "metric_y2 = {\n",
        "    'metric': 'y2',\n",
        "    'goal': 'MAXIMIZE'\n",
        "}\n",
        "\n",
        "# Put it all together in a study configuration\n",
        "study_config = {\n",
        "    'algorithm': 'ALGORITHM_UNSPECIFIED',  # Let the service choose the `default` algorithm.\n",
        "    'parameters': [param_r, param_theta, ],\n",
        "    'metrics': [metric_y1, metric_y2, ],\n",
        "}\n",
        "\n",
        "print(json.dumps(study_config, indent=2, sort_keys=True))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "uyXG_RKha7Kb"
      },
      "source": [
        "### Create the study\n",
        "\n",
        "Next, create the study, which you will subsequently run to optimize the two objectives."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "jgskzqZX0Mkt"
      },
      "outputs": [],
      "source": [
        "service = tuner.optimizer_client.create_or_load_study(PROJECT_ID, REGION, STUDY_ID, study_config)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "dKOMBKmtkcJb"
      },
      "source": [
        "### Metric evaluation functions\n",
        "\n",
        "Next, define some functions to evaluate the two objective metrics."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Xnl1uqnyz3Qp"
      },
      "outputs": [],
      "source": [
        "import math\n",
        "\n",
        "\n",
        "# r * sin(theta)\n",
        "def Metric1Evaluation(r, theta):\n",
        "    \"\"\"Evaluate the first metric on the trial.\"\"\"\n",
        "    return r * math.sin(theta)\n",
        "\n",
        "\n",
        "# r * cos(theta)\n",
        "def Metric2Evaluation(r, theta):\n",
        "    \"\"\"Evaluate the second metric on the trial.\"\"\"\n",
        "    return r * math.cos(theta)\n",
        "\n",
        "\n",
        "def CreateMetrics(trial_id, params):\n",
        "    print((\"=========== Start Trial: [{0}] =============\").format(trial_id))\n",
        "\n",
        "    r = params['r']\n",
        "    theta = params['theta']\n",
        "\n",
        "    # Evaluate both objective metrics for this trial\n",
        "    y1 = Metric1Evaluation(r, theta)\n",
        "    y2 = Metric2Evaluation(r, theta)\n",
        "    print('[r = {0}, theta = {1}] => y1 = r*sin(theta) = {2}, y2 = r*cos(theta) = {3}'.format(r, theta, y1, y2))\n",
        "    metric1 = {'metric': 'y1', 'value': y1}\n",
        "    metric2 = {'metric': 'y2', 'value': y2}\n",
        "\n",
        "    # Return the results for this trial\n",
        "    return [metric1, metric2, ]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Qzn5lVpRq05U"
      },
      "source": [
        "### Set configuration parameters for running trials\n",
        "\n",
        "__`client_id`__ - The identifier of the client that is requesting the suggestion. If multiple SuggestTrialsRequests have the same `client_id`, the service will return the identical suggested trial if the trial is `PENDING`, and provide a new trial if the last suggested trial was completed.\n",
        "\n",
        "__`suggestion_count_per_request`__ - The number of suggestions (trials) requested in a single request.\n",
        "\n",
        "__`max_trial_id_to_stop`__ - The number of trials to explore before stopping. It is set to 4 to shorten the time to run the code, so don't expect convergence. For convergence, it would likely need to be about 20 (a good rule of thumb is to multiply the total dimensionality by 10).\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "5usXaZA5qvUZ"
      },
      "outputs": [],
      "source": [
        "client_id = 'client1' #@param {type: 'string'}\n",
        "suggestion_count_per_request = 5 #@param {type: 'integer'}\n",
        "max_trial_id_to_stop = 4 #@param {type: 'integer'}\n",
        "\n",
        "print('client_id: {}'.format(client_id))\n",
        "print('suggestion_count_per_request: {}'.format(suggestion_count_per_request))\n",
        "print('max_trial_id_to_stop: {}'.format(max_trial_id_to_stop))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "UnV2SJNskm7V"
      },
      "source": [
        "### Run AI Platform Optimizer trials\n",
        "\n",
        "Run the trials."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "opmuTntW4-eS"
      },
      "outputs": [],
      "source": [
        "trial_id = 0\n",
        "while trial_id < max_trial_id_to_stop:\n",
        "    suggestions = service.get_suggestions(client_id, suggestion_count_per_request)\n",
        "    for suggested_trial in suggestions[\"trials\"]:\n",
        "        trial_id = int(utils.get_trial_id(suggested_trial))\n",
        "\n",
        "        trial = service.get_trial(trial_id)\n",
        "        if trial['state'] in ['COMPLETED', 'INFEASIBLE']:\n",
        "            continue\n",
        "\n",
        "        # Parses the suggested parameters.\n",
        "        params = utils.convert_optimizer_trial_to_dict(suggested_trial)\n",
        "\n",
        "        service.report_intermediate_objective_value(\n",
        "            step=1, elapsed_secs=0, metric_list=CreateMetrics(trial_id, params), trial_id=trial_id)\n",
        "        service.complete_trial(trial_id, trial_infeasible=False)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "i5ZTqgqBiRsq"
      },
      "source": [
        "### [EXPERIMENTAL] Visualize the result\n",
        "\n",
        "This section provides a module to visualize the trials for the above study.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Or2PL1YxTr33"
      },
      "outputs": [],
      "source": [
        "max_trials_to_annotate = 20\n",
        "\n",
        "import matplotlib.pyplot as plt\n",
        "trial_ids = []\n",
        "y1 = []\n",
        "y2 = []\n",
        "trials = service.list_trials()\n",
        "for trial in trials:\n",
        "    if 'finalMeasurement' in trial:\n",
        "        trial_ids.append(int(trial['name'].split('/')[-1]))\n",
        "        metrics = trial['finalMeasurement']['metrics']\n",
        "        try:\n",
        "            y1.append([m for m in metrics if m['metric'] == \"y1\"][0]['value'])\n",
        "            y2.append([m for m in metrics if m['metric'] == \"y2\"][0]['value'])\n",
        "        except Exception:\n",
        "            pass\n",
        "\n",
        "fig, ax = plt.subplots()\n",
        "ax.scatter(y1, y2)\n",
        "plt.xlabel(\"y1=r*sin(theta)\")\n",
        "plt.ylabel(\"y2=r*cos(theta)\")\n",
        "for i, trial_id in enumerate(trial_ids):\n",
        "    # Only annotates the last `max_trials_to_annotate` trials\n",
        "    if i > len(trial_ids) - max_trials_to_annotate:\n",
        "        try:\n",
        "            ax.annotate(trial_id, (y1[i], y2[i]))\n",
        "        except Exception:\n",
        "            pass\n",
        "plt.gcf().set_size_inches((16, 16))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "KAxfq9Fri2YV"
      },
      "source": [
        "## Cleaning up\n",
        "\n",
        "To clean up all Google Cloud resources used in this project, you can [delete the Google Cloud\n",
        "project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#shutting_down_projects) you used for the tutorial."
      ]
    }
  ],
  "metadata": {
    "colab": {
      "collapsed_sections": [],
      "name": "ai_platform_optimizer_multi_objective.ipynb",
      "toc_visible": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
